<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property integer $active
 *
 * The followings are the available model relations:
 * @property Employee $employee
 * @property UserAccess $userAccess
 */

class User extends CActiveRecord {
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */

	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */

	public function tableName() {
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */

	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'active',
				'numerical',
				'integerOnly' => true
			),
			array(
				'username',
				'required',
			),
			array(
				'password',
				'required',
				'on' => 'create'
			),
			array(
				'username',
				'unique',
			),
			array(
				'username, password',
				'length',
				'max' => 255
			),
			array(
				'password',
				'length',
				'min' => 4
			),
			array(
				'username, password',
				'unsafe',
				'on' => 'update'
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, username, active',
				'safe',
				'on' => 'search'
			),
		);
	}

	/**
	 * @return array relational rules.
	 */

	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'employee' => array(
				self::HAS_ONE,
				'Employee',
				'userId'
			),
			'userAccess' => array(
				self::HAS_ONE,
				'UserAccess',
				'userId'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */

	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'username' => '用户名',
			'password' => '密码',
			'active' => '激活状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */

	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('username', $this->username, true);
		$criteria->compare('password', $this->password, true);
		$criteria->compare('active', $this->active);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}

	public function save($runValidation = true, $attributes = null) {
		$result = parent::save($runValidation, $attributes);

		if ($result && $this->userAccess == null) {
			$userAccess = new UserAccess();
			$userAccess->userId = $this->id;
			$userAccess->save();
		}

		return $result;
	}
}
